package com.xingkeduo.pojo.json.cms;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.table.PermsTable;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.tool.CommonUtil;

import java.text.DateFormat;
import java.util.Iterator;
import java.util.List;

public class MenuJson extends BaseJson {

	public static ObjectNode toJson(PermsTable permsTable) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (permsTable != null)
		{
			root.put("id", permsTable.getId());
			root.put("pid", permsTable.getPid());
			root.put("title", permsTable.getName());
			root.put("boardCode", permsTable.getBoardCode());
			root.put("type", permsTable.getType());
			root.put("value", permsTable.getValue());
			root.put("url", permsTable.getUrl());
			root.put("sort", permsTable.getSort());
			root.put("font", permsTable.getFont());
			root.put("icon", permsTable.getIcon());
			root.put("sort", permsTable.getSort());
			root.put("children", toJson(permsTable.getChildren()));
			//处理时间
			String createdTime = DateFormat.getDateTimeInstance().format(permsTable.getCreatedTime());//可以精确到时分秒
			root.put("createtime", createdTime);

			String updateTime = DateFormat.getDateTimeInstance().format(permsTable.getUpdatedTime());//可以精确到时分秒
			root.put("updatetime", updateTime);
		}

		return root;
	}

	public static ArrayNode toJson(List<PermsTable> tables) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if (!CommonUtil.isEmpty(tables))
			for (Iterator<PermsTable> iterator = tables.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}
		return root;
	}


//	public static ArrayNode toJson(List<PermsTable> permsTableList) throws Throwable {
//		ArrayNode root = MAPPER.createArrayNode();
//		return root.add(JSON.toJSONString(permsTableList));
//	}


}
